/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.pip;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.media.image.common.PixelFormat;
import ohos.multimodalinput.event.TouchEvent;

import com.yc.video.ResourceTable;
import com.yc.video.tool.PlayerUtils;

/**
 * 悬浮窗控件（解决滑动冲突）
 *
 * @since 2021-05-10
 */
public class FloatVideoView extends StackLayout implements Component.TouchEventListener {
    private WindowManager mWindowManager;
    private WindowManager.LayoutConfig mParams;
    private int mDownX; // 手指按下时相对于悬浮窗的坐标
    private int mDownY; // 手指按下时相对于悬浮窗的坐标

    /**
     * FloatVideoView
     *
     * @param context context
     * @param downx downx
     * @param downy downy
     */
    public FloatVideoView(Context context, int downx, int downy) {
        super(context);
        mDownX = downx;
        mDownY = downy;
        init();
    }

    private void init() {
        setTouchEventListener(this);
        setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_shape_float_window_bg));
        int padding = AttrHelper.fp2px(1, getContext());
        setPadding(padding, padding, padding, padding);
        initWindow();
    }

    private void initWindow() {
        mWindowManager = PlayerUtils.getWindowManager();
        mParams = new WindowManager.LayoutConfig();
        mParams.type = WindowManager.LayoutConfig.MARK_ALT_FOCUSABLE_IM;
        mParams.pixelFormat = PixelFormat.ARGB_8888.getValue(); // 设置图片格式，效果为背景透明
        mParams.flags = WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE;
        mParams.alignment = LayoutAlignment.START | LayoutAlignment.TOP; // 调整悬浮窗口至右下角
        int width = AttrHelper.fp2px(250, getContext()); // 设置悬浮窗口长宽数据
        mParams.width = width;
        mParams.height = width * 9 / 16;
        mParams.x = mDownX;
        mParams.y = mDownY;
    }

    /**
     * 添加至窗口
     *
     * @return return
     */
    public boolean addToWindow() {
        if (mWindowManager != null) {
            if (getComponentParent() == null) {
                mWindowManager.addComponent(this, getContext(), -1);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 从窗口移除
     *
     * @return return
     */
    public boolean removeFromWindow() {
        if (mWindowManager != null) {
            if (getComponentParent() != null) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        return false;
    }
}
